package Cantella::DJabberd::Model::Admin;

use Reaction::InterfaceModel::ObjectClass;
use Scalar::Util ();
use aliased 'Reaction::InterfaceModel::Reflector::DBIC' => 'Reflector';

use aliased 'Cantella::DJabberd::Model::Admin::User' => 'UserObject';

class Admin, which {

  does 'Cantella::DJabberd::Role::CurrentUser';

  implements BUILD => as {
    my ($self) = @_;
    $self->_setup_current_user_sync;
  };

  implements _setup_current_user_sync => as {
    my ($self) = @_;
    my $weak_self = $self;
    Scalar::Util::weaken($weak_self);
    my $cb = sub {
      my ($dm_user) = @_;
      return $weak_self->_clear_current_user unless defined $dm_user;
      #restrict schema with unrestricted user, then restrict user with restricted schema. circles!
      my $restricted = $weak_self->djabberd_schema->restrict_with_object( $dm_user );
      $weak_self->djabberd_schema( $restricted );
      $dm_user->result_source( $restricted->source(ref $dm_user) );
      #now create the IM user and set it
      my $im_user = UserObject->new(_user_store => $dm_user);
      $weak_self->_set_current_user( $im_user );
    };
    # set callback and call it now if there's already a current user
    my $dm = $self->djabberd_schema;
    $dm->current_user_callback($cb);
    $cb->($dm->current_user) if $dm->has_current_user;
  };
};


#collection action distinction todo
my $reflector = Reflector->new();
$reflector->reflect_schema
  (
   model_class => 'Cantella::DJabberd::Model::Admin',
   schema_class => 'Cantella::DJabberd::Store',
   domain_model_name => 'djabberd_schema',
   sources =>
   [
    [ User => #M2M
      {
       reader => 'users',
       domain_model_args =>
       { handles => [qw/check_password check_roles check_role check_any_role/] },
       attributes => [qw/id username password full_name jids 
                         created_d modified_d deleted_d/
                     ],
       actions => [ [ Update => {attributes => [qw/username full_name deleted_d/] } ],],
       collection =>
       { actions =>
         [
          [ Create => {attributes => [qw/username password full_name/] } ],
         ],
       }
      },
    ],
    [ Role => #M2M
      {
       reader => 'roles',
       attributes => [qw/id name description/],
       actions =>
       [ 'Delete',
         [ Update => {attributes => [qw/name description/], }, ],
       ],
       collection =>
       { actions =>
         [
          'DeleteAll',
          [ Create => {attributes => [qw/name description/], }, ],
         ],
        },
      },
    ],
    [ Vhost => #DONE
      {
       reader => 'vhosts',
       attributes => [qw/id hostname external_components/],
       actions => [ qw/Update Delete/ ],
       collection => { actions => [qw/Create DeleteAll/] }
      },
    ],
    [ UserJid => #M2M
      {
       reader => 'user_jids',
       attributes => [ qw/id jid user node domain roster_groups roster_items
                         messages sent_messages recieved_messages/ ],
       actions => [ 'Delete', [ Update => { attributes => [] } ], ],
       collection =>
       {
        actions => [ [ Create => { attributes => [qw/user node domain/]}, ],],
       },
      },
    ],
    [ JabberGroup => #M2M
      {
       reader => 'jabber_groups',
       attributes => [qw/id name description/],
       actions =>
       [ 'Delete',
         [ Update => {attributes => [qw/name description/], }, ],
       ],
       collection =>
       { actions =>
         [
          'DeleteAll',
          [ Create => {attributes => [qw/name description/], }, ],
         ],
        },
      },
    ],
    [ LoggedMessage => #DONE
      {
       reader => 'logged_messages',
       attributes => [qw/id message delivery_d to from/],
       actions => [],
       collection => { actions => [] },
      },
    ],
    [ ExternalComponent => #DONE MAYBE
      {
       reader => 'external_components',
       attributes => [qw/id hostname port secret vhost/],
       actions => [
                   'Delete',
                   [ Update => { attributes => [qw/hostname secret port/] } ],
                  ],
       collection => { actions => [qw/Create DeleteAll/] },
      }
    ],
    [ RosterGroup => #M2M
      {
       reader => 'roster_groups',
       attributes => [qw/id name owner/],
       actions => [
                   'Delete',
                   [ Update => { attributes => [qw/name/] } ],
                  ],
       collection =>
       {
        actions => [
                    'DeleteAll',
                    [ Create => { attributes => [qw/name owner/] } ],
                   ],
       },
      },
    ],
    [ RosterItem =>
      {
       reader => 'roster_items',
       attributes => [qw/owner name contact subscription/],
       actions => [
                   'Delete',
                   [ Update => { attributes => [qw/name subscription/] } ],
                  ],
       collection =>
       {
        actions =>
        [
         'DeleteAll',
         [ Create => { attributes => [qw/name owner contact subscription/] } ],
        ],
       },
      }
    ],
   ]
  );


1;
